package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.HisDetailActivity;
import com.xgkj.diyiketang.activity.SearchAnchorActivity;
import com.xgkj.diyiketang.activity.data.MingShiAdapter;
import com.xgkj.diyiketang.activity.data.SubjectAdapter;
import com.xgkj.diyiketang.activity.data.XueXiaoAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.SchoolInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.task.SchoolListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.PopupHelper;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class Fragment_MingShi extends BaseFragment implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener, View.OnClickListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private MingShiAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int paihangbangTaskId = -1;
    private ImageView sousuo;
    private RelativeLayout chooseXueXiao;
    private TextView xuexiaoText;

    private RelativeLayout chooseKemu;
    private TextView kemu;
    private PopupHelper mSubjectPopWindow;
    private SubjectAdapter mSubjectAdapter;
    private SubjectAdapter mGradeAdapter;
    private XueXiaoAdapter xAdapter;
    private String mSubjectNo;
    private SchoolInfo sqInfo;
    private PopupHelper xuexiaoPopWindow;
    private PopupHelper mGradePopWindow;
    private String mSchool;
    private int schoolListTaskId = -1;
    private NoDataUtil noDataUtil;
    private TextView nianji;
    private String mGrade;
    private RelativeLayout chooseNianji;

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mingshi_list, null);
        mContext = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        sousuo = (ImageView) view.findViewById(R.id.mingshi_search);
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, SearchAnchorActivity.class));
            }
        });

        chooseXueXiao = (RelativeLayout) view.findViewById(R.id.choose_xuexiao);
        chooseXueXiao.setOnClickListener(this);
        xuexiaoText = (TextView) view.findViewById(R.id.xuexiao);

        chooseKemu = (RelativeLayout) view.findViewById(R.id.choose_kemu);
        chooseKemu.setOnClickListener(this);
        kemu = (TextView) view.findViewById(R.id.kemu);

        chooseNianji = (RelativeLayout) view.findViewById(R.id.choose_nianji);
        chooseNianji.setOnClickListener(this);
        nianji = (TextView) view.findViewById(R.id.nianji);


        mListView = (PullToRefreshListView) view.findViewById(R.id.mingshi_list);
        mListView.init(mContext, true);
        mAdapter = new MingShiAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //PalLog.e("位置",position+"");
                if (mListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0 || position >= mAdapter.getCount()) return;
                EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
//				startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
                String uid = info.getUserId();

                Intent intent = new Intent(mContext, HisDetailActivity.class);
                intent.putExtra("USERID", uid);

                startActivity(intent);
            }
        });
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        initPopWindow();
        getSchoolList();
        View empty_view = view.findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext, empty_view);
        mListView.doRefresh();

    }



    private void initPopWindow() {
        //年纪
        mGradeAdapter = new SubjectAdapter(mContext,2);
        mGradePopWindow = new PopupHelper(mContext,mGradeAdapter);
        mGradePopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                nianji.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallxiajiantou,0);
            }
        });
        mGradePopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mGradePopWindow.dismissDropDownView();
                if (position == 0){
                    mGrade = null;
                }else {
                    mGrade = String.valueOf(position);
                }
                nianji.setText((String)mGradeAdapter.getItem(position));
                mGradeAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });

        //科目popupwindow
        mSubjectAdapter = new SubjectAdapter(mContext, 1);
        mSubjectAdapter.setSelectPosition(0);
        kemu.setText((String) mSubjectAdapter.getItem(0));
        mSubjectPopWindow = new PopupHelper(mContext, mSubjectAdapter);
        mSubjectPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                kemu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });
        mSubjectPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSubjectPopWindow.dismissDropDownView();
                if (position == 0) {
                    mSubjectNo = null;
                } else {
                    mSubjectNo = String.valueOf(position - 1);
                }
                kemu.setText((String) mSubjectAdapter.getItem(position));
                mSubjectAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });

        xAdapter = new XueXiaoAdapter(mContext);
        xuexiaoPopWindow = new PopupHelper(mContext, xAdapter);
        xuexiaoPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                xuexiaoText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });
        xuexiaoPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                xuexiaoPopWindow.dismissDropDownView();
                sqInfo = (SchoolInfo) xAdapter.getItem(position);
                if (sqInfo != null) {
                    mSchool = sqInfo.getName();
                }
                xuexiaoText.setText(mSchool);
                if(position==0){
                    mSchool = null;
                }
                xAdapter.setSelectPosition(position);
                mListView.doRefresh();
            }
        });

        xuexiaoPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                xuexiaoText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallxiajiantou, 0);
            }
        });


    }


    private void getPaiHaiBang() {
        if (paihangbangTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }

        AnchorListTask task = new AnchorListTask(size, currentPage, "",mSchool,mSubjectNo, "0",mGrade);
        task.addListener(new AnchorListener(mContext));
        paihangbangTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private void getSchoolList(){
        SchoolListTask schoolListTask = new SchoolListTask();
        schoolListTask.addListener(new AnchorListener(mContext));
        schoolListTaskId = getEatShowApp().getTaskManager().addTask(schoolListTask);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_kemu:
                kemu.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                mSubjectPopWindow.showDropDownView(v);
                break;

            case R.id.choose_xuexiao:
                xuexiaoText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.smallshangjiantou, 0);
                xuexiaoPopWindow.showDropDownView(v);
                break;

            case R.id.choose_nianji:
                nianji.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallshangjiantou,0);
                mGradePopWindow.showDropDownView(v);
                break;
        }
    }

    class AnchorListener extends ShowToastTaskEventListener {
        public AnchorListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == paihangbangTaskId) {
                paihangbangTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                } else {
                    mListView.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if (data != null) {
                    fansList = (List<EatShowUserInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList);
                }
                if (mAdapter != null && mAdapter.getCount()>0){
                    noDataUtil.ListViewNoEmpty(mListView);
                }else {
                    noDataUtil.ListViewEmpty(mListView,null,null,-1);
                }
                currentPage = result;

            }else if (taskId == schoolListTaskId){
                if (data != null){
                    List<SchoolInfo> schoolInfos = (List<SchoolInfo>) data;
                    if (schoolInfos != null && schoolInfos.size()>0){
                        xAdapter.clearData();
                        xAdapter.updateData(schoolInfos);
                    }
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == paihangbangTaskId) {
                paihangbangTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //mListView.invalidate();
            if (mAdapter != null && mAdapter.getCount() == 0) {
                mListView.doRefresh();
            }

        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPaiHaiBang();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getPaiHaiBang();

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("PaiHangFragment");
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("PaiHangFragment");
    }
}
